封装
封装:将类的某些信息隐藏在类的内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问
- 隐藏实际细节,提供公共的访问方式,提高了代码复用性、安全性
- 可以对封装的变量进行属性检测
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31//此处仅封装了属性,同样可以利用private实现私有方法(后面介绍:封装构造方法<单例模式>)
class Employee {
private String name; //封装属性,只需要加上访问控制符private
private double salary;
public String getName() { //提供getter/setter方法供外部访问
return name;
}
public void setName(String n) {
name = n;
}
public double getSalary() {
return salary;
}
public void setSalary(double s) {
if(s >= 0) { //检测输入的s
salary = s;
}
}
public void info() {
System.out.println("姓名:" + getName() + ";薪资:" + getSalary());
}
}
public class EncapsulationTest {
public static void main(String[] args) {
Employee emp = new Employee();
//emp.name = "张三"; //name私有,只能通过setName设置
emp.setName("张三");
emp.setSalary(-1000); //加入检测功能,负数不满足条件,salary为默认值
emp.info();
}
}
this关键字
- 非构造方法中,用来表示当前对象,调用成员变量/方法或返回自身
- 构造方法中,用来调用另一个构造方法,必须放在方法首行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46class Employee {
private String name;
private double salary;
public Employee(String name) {
this("张三",25); //this调用构造方法,必须放在方法首行!
this.name = name;
}
public Employee(String name,double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
//public void setName(String n) { //n表示参数意义不明确
// name = n;
//}
//public void setName(String name) { //局部变量与成员变量重名,方法内部优先使用局部变量,程序错误
// name = name;
//}
public void setName(String name) { //形参name值传递给当前对象.成员变量
this.name = name; //用this关键字表示当前对象或当前类的一个实例,用this调用对象的所有方法和属性
}
public double getSalary() {
return salary;
}
public void setSalary(double s) {
salary = s;
}
public void info() {
System.out.println("姓名:" + getName() + ";薪资:" + getSalary());
}
public Employee returnthis() { //返回Employee类
return this; //return this返回当前对象
}
}
public class ThisTest {
public static void main(String[] args) {
Employee emp = new Employee("张三"); //构造方法对实例域初始化(张三),后面更改要用set
emp.setName("李四");
//emp.setSalary(20);
emp.info(); //姓名:李四;薪资:25.0
System.out.println(emp.returnthis()); //part023.Employee@70dea4e
}
}
访问修饰符:Java通过访问修饰符来限制类、属性和方法的访问权限,通常放在语句的最前端
- public:共有的,对所有类可见,如果分布在不同包。需要导包
- protected:受保护的,对同一包内的类和所有子类(无论在不在同一包内)可见
- (默认):同一包内可见
- private: 私有的,仅本类可见
继承
- 继承:子类继承父类,继承除构造方法外所有属性和方法,并可以在此基础上拓展,提高代码复用性;”is-a”关系是继承的明显特征。
定义子类:关键字extends表示继承,子类比超类拥有的功能更加丰富
1
2
3
4
5
6
7
8
9
10class Employee { //定义父类Employee
private String name;
private double salary;
getter、setter方法...
}
class Manager extends Employee { //定义子类Manager,继承自Employee
private double bonus; //拓展定义bonus属性
getter、setter方法...
}覆写 override 区别方法重载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15class Employee { //定义父类Employee
private String name;
private double salary;
getter、setter方法...
}
class Manager extends Employee { //定义子类Manager,继承自Employee
private double bonus; //拓展定义bonus属性
getter、setter方法...
public double getSalary() { //此处覆写getsalary方法
//return salary + bonus; 报错:不能直接访问salary域
//return getSalary() + bonus; 报错:无限调用自身,直至崩溃
return super.getSalary() + bonus; //关键字super表示调用父类的getSalary()方法
}
}子类对象的实例化过程 隐藏super语句 this与super区别
- 子类并没有继承父类的构造方法,只是子类实例化对象时,会默认先调用父类的无参构造器(相当于默认super())
- 如果想调用父类的其他带参构造器,需要在子类构造器首行添加super()语句!
如果父类没有无参构造器,而子类的构造器中又没有显式调用父类的其他构造器,则编译错误
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15class Emoloyee {
String name;
double salary;
public Emoloyee() { //父类无参构造方法
System.out.println("父类Employee的无参构造方法");
}
}
class Manager extends Emoloyee {
}
public class InheritanceTest {
public static void main(String[] args) {
//对象实例化中,调用了父类的无参构造方法,是否说明子类继承了父类构造方法?
Manager m = new Manager(); //输出:父类Employee的无参构造方法
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22class Emoloyee {
String name;
double salary;
public Emoloyee() { //父类无参构造方法
System.out.println("父类Employee的无参构造方法");
}
public Employee(String name) {
System.out.println("父类Employee的有参构造方法");
}
}
class Manager extends Emoloyee {
public Manager(String name) {
super(name);
System.out.println("子类Manager的有参构造方法");
}
}
public class InheritanceTest {
public static void main(String[] args) {
//对象实例化中,调用了父类的有参构造方法和自身的有参构造方法
Manager m = new Manager("张三"); 输出:父类Employee的有参构造方法 子类Manager的有参构造方法
}
}
super关键字:
- 继承层次:继承可以多层继承,比如C继承自B,B继承自A;但不允许多继承,即C既继承A,也继承B;关于Java多继承的实现方式,参考接口内容。
- final
- 强制类型转换
- 抽象类:
Object:所有类的超类
Object:Object类是Java中所有的基类,如果在类的声明中未使用extends关键字指明其基类,则默认基类为Object类;
1
2
3
4
5
6
7public class Person{
...
}
等价于
public class Person extends Object {
...
}equals方法
- 深入==与equals
- hashCode方法
- toString方法
多态
- 对象转型(具有继承关系)
- 向上转型:子类对象转换为父类对象(Employee->Manager)
- 向下转型:父类对象转换为子类对象(Manager->Employee)
1
2
- 动态绑定与多态